package com.btpn.custody.security.action;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.btpn.custody.security.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.ExpressionValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;

@ParentPackage(value="custody")
@Results({
	@Result(name="input", location="t_change_password")
})
@Validations(
	requiredStrings = {
		@RequiredStringValidator(type=ValidatorType.SIMPLE, fieldName="oldPassword", message="Old password must be filled"),
		@RequiredStringValidator(type=ValidatorType.SIMPLE, fieldName="newPassword", message="New password must be filled")
    },
    stringLengthFields = {
		@StringLengthFieldValidator(type=ValidatorType.SIMPLE, minLength="5", maxLength="20", fieldName="newPassword", message="New assword must contain at ${minLength} until ${maxLength} characters")
	},
	expressions = {
        @ExpressionValidator(expression="newPassword == confirmNewPassword", message="New password and confirm new password do not match")
    }
)
public class ChangePasswordAction extends ActionSupport {
	
	private static final long serialVersionUID = -7561060017101134762L;
	
	@Autowired
	private IUserService userService;
	
	private String oldPassword;
	private String newPassword;
	private String confirmNewPassword;
	
//	@SkipValidation
//	@Action(value="/showChangePassword", results={ 
//			@Result(name="success", location="t_change_password"), 
//	})
//	public String showChangePassword() throws Exception {
//		
//		return SUCCESS;
//	}
	
//	@Action(value = "/changePassword", results = { 
//			@Result(name="success", location="t_home"),			
//			@Result(name="error", type="redirectAction", params = {"actionName", "showChangePassword"})
//	})
//	public String changePassword() throws Exception {
//		
//		// validate password regex
//		if (!MyRegexValidator.validatePassword(newPassword)) {
//			addFieldError("password", "New Password must contain 0-9, a-z and length 5-20 characters");
//			return INPUT;
//		}
//		
//		String result = userService.changePassword(oldPassword, newPassword);
//		if (result.contains("Success")) {
//			addActionMessage(result);
//		} else {
//			addActionError(result);
//		}
//		
//		return SUCCESS;
//	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmNewPassword() {
		return confirmNewPassword;
	}

	public void setConfirmNewPassword(String confirmNewPassword) {
		this.confirmNewPassword = confirmNewPassword;
	}
}
